package com.by.service;

import com.by.entity.PageResult;
import com.by.entity.QueryPageBean;
import com.by.mapper.CheckGropMapper;
import com.by.pojo.CheckGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGropMapper checkGropMapper;

    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //返回自增id
        checkGropMapper.addCheckGroup(checkGroup);
        addCheckGroupCheckItem(checkitemIds, checkGroup);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页条件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //去空格，避免不必要的条件查询
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString() != "") {
            queryPageBean.setQueryString(queryPageBean.getQueryString().trim());
        }
        //正常的查询
        List<CheckGroup> checkGroupList = checkGropMapper.findPage(queryPageBean);
        //获取分页信息
        PageInfo<CheckGroup> pageInfo = new PageInfo<CheckGroup>(checkGroupList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGropMapper.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGropMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        //1、保存检查组信息
        checkGropMapper.edit(checkGroup);


        //2、根据checkGroupId删除关联表
        checkGropMapper.deleteCheckGroupItemByCheckGroupId(checkGroup.getId());

        //3、保存关联表信息
        addCheckGroupCheckItem(checkitemIds, checkGroup);
    }

    /**
    * @Description: 按照检查组id删除检查组
            * @Param: * @param id
            * @return: void
            * @Author: GP
            * @Date: 2024/1/10
            */
    @Override
    public void delete(Integer id) {
        checkGropMapper.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGropMapper.findAll();
    }

    /**
     * @param checkGroup
     * @Description: 保存关联表信息 注意：ctrl+alt+m
     * @Param: * @param checkitemIds
     * @return: void
     * @Author: GP
     * @Date: 2024/1/10
     */
    private void addCheckGroupCheckItem(Integer[] checkitemIds, CheckGroup checkGroup) {
        Integer checkGroupId = checkGroup.getId();
        for (Integer checkitemId : checkitemIds) {
            checkGropMapper.addCheckGroupCheckItem(checkGroupId, checkitemId);
        }
    }

//    public static void main(String[] args) {
//        String str = " ";
//        str = str.trim();
//        System.out.println(str.length());
//    }
}
